#include <iostream>
#include "itemwindow.h"

ItemWindow::ItemWindow()
{
	set_title("Gtk::TreeView (ListStore) example");
	set_border_width(5);
	set_default_size(800, 600);

	add(m_VBox);


	/// ListStore
	m_ScrolledWindow.set_shadow_type(Gtk::SHADOW_NONE);
	m_ScrolledWindow.add(m_TreeView);

	m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

	m_VBox.pack_start(m_ScrolledWindow);

	m_refTreeModel = Gtk::ListStore::create(m_Columns);
	m_TreeView.set_model(m_refTreeModel);

/*
	Gtk::TreeModel::Row row = *(m_refTreeModel->append());

	row[m_Columns.m_col_id] = 1;
	row[m_Columns.m_col_name] = "HJ Kim";
	row[m_Columns.m_col_number] = 10;
	row[m_Columns.m_col_percentage] = 15;

	row = *(m_refTreeModel->append());
	row[m_Columns.m_col_id] = 2;
	//row[m_Columns.m_col_name] = "SP Kim & Blah Blah Blah Blah Blah Blah Blah ~";
	row[m_Columns.m_col_name] = "SP Kim";
	row[m_Columns.m_col_number] = 1;
	row[m_Columns.m_col_percentage] = 50;

	row = *(m_refTreeModel->append());
	row[m_Columns.m_col_id] = 3;
	row[m_Columns.m_col_name] = "YD JA";
	row[m_Columns.m_col_number] = 20;
	row[m_Columns.m_col_percentage] = 40;

	row = *(m_refTreeModel->append());
	row[m_Columns.m_col_id] = 4;
	row[m_Columns.m_col_name] = "YD JA";
	row[m_Columns.m_col_number] = 20;
	row[m_Columns.m_col_percentage] = 40;
*/

	m_TreeView.append_column("ID", m_Columns.col_id);
	m_TreeView.append_column("NAME", m_Columns.col_name);

/*
	for(guint i=0; i < 1; i++)
	{
		Gtk::TreeView::Column* pColumn = m_TreeView.get_column(i);
		pColumn->set_reorderable();
	}
*/


	m_VBox.pack_start(m_HBox, Gtk::PACK_SHRINK);

	/// Entry
	m_Entry.set_max_length(40);
	m_Entry.set_text("");
	//m_Entry.set_text(m_Entry.get_text()); // m_Entry.get_text()
	m_Entry.select_region(0, m_Entry.get_text_length());
	m_Entry.signal_activate().connect( sigc::mem_fun(*this, &ItemWindow::on_button_add_clicked)/*, m_Entry.get_text() */);
	m_Entry.set_activates_default(true);

	m_Entry.grab_focus();
	m_HBox.pack_start(m_Entry);


	/// Button
	m_Button_AddItem.set_label("Add");
	m_Button_AddItem.signal_clicked().connect( sigc::mem_fun(*this, &ItemWindow::on_button_add_clicked)/*, m_Entry.get_text() */);
	//m_Button_AddItem.signal_enter().connect( sigc::mem_fun(*this, &ItemWindow::on_button_add_clicked)/*, m_Entry.get_text() */);
	m_HBox.pack_start(m_Button_AddItem);
	m_Button_AddItem.grab_default();

	m_Button_RemoveItem.set_label("Remove");
	m_Button_RemoveItem.signal_clicked().connect( sigc::mem_fun(*this, &ItemWindow::on_button_remove_clicked)/*, m_Entry.get_text() */);
	m_HBox.pack_start(m_Button_RemoveItem);

	m_Button_PrintItem.set_label("Print It!");
	m_Button_PrintItem.signal_clicked().connect( sigc::mem_fun(*this, &ItemWindow::on_button_print_clicked)/*, m_Entry.get_text() */);
	m_HBox.pack_start(m_Button_PrintItem);



	m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);

	m_Button_Quit.set_label("Quit");
	m_ButtonBox.pack_start(m_Button_Quit, Gtk::PACK_SHRINK);
	m_ButtonBox.set_border_width(5);
	m_ButtonBox.set_layout(Gtk::BUTTONBOX_END);
	m_Button_Quit.signal_clicked().connect( sigc::mem_fun(*this, &ItemWindow::on_button_quit) );


	show_all_children();
}

ItemWindow::~ItemWindow()
{
}


CellItem_Product::CellItem_Product()
{
  m_col_id = 0;
}

CellItem_Product::CellItem_Product(const CellItem_Product& src)
{
  operator=(src);
}

CellItem_Product::~CellItem_Product()
{
}

CellItem_Product& CellItem_Product::operator=(const CellItem_Product& src)
{
  m_col_id= src.m_col_id;
  m_col_name = src.m_col_name;

  return *this;
}


void ItemWindow::on_button_quit()
{
	hide();
}

void ItemWindow::liststore_add_item(const CellItem_Product& foo)
{
  //Gtk::TreeRow row = *(m_refListStore->append());
  Gtk::TreeModel::Row row = *(m_refTreeModel->append());

  row[m_Columns.col_id]   = foo.m_col_id;
  row[m_Columns.col_name]   = foo.m_col_name;
  
  std::cout << "Added Item! [ " << row[m_Columns.col_id] << " ][ " << row[m_Columns.col_name] << " ]\n" << std::endl; 
}

void ItemWindow::on_button_print_clicked(/* Glib::ustring getValue */)
{
	int i;
	char str_log[60]="";

	FILE *pfile_read;
	FILE *pfile_write;

	//char* product_list = (char*)malloc(sizej

	pfile_read = fopen(LIST_LOG_LOCATE, "rt");
	if(pfile_read == NULL)
	{
		printf("log file not found......\n");
		pfile_read = fopen(LIST_LOG_LOCATE, "a+t");
	}

	for(i=0;i < index_count;i++)
	{
		if(feof(pfile_read))
			break;

		fgets(str_log, sizeof(str_log), pfile_read);
		std::cout << "i : [ " << i << " ]\tValue :  "<< str_log << std::endl; //
	}


	/*

	pfile_write = fopen(LIST_LOG_LOCATE, "a+t");
	if(pfile_write == NULL)
		printf("Fail to open %s file!\n", LIST_LOG_LOCATE);


	sprintf(str_log, "%4d\t%s", foo.m_col_id, foo.m_col_name.data());
	fprintf(pfile_write, "%s\n", str_log);

	fclose(pfile_write);

	*/


	pfile_write = fopen(LIST_LOG_LOCATE, "w+t");
	fclose(pfile_write);
	pfile_read = fopen(LIST_TMP_LOCATE, "w+t");
	fclose(pfile_read);

	/* Delete all items from ListStore */
  	//m_refTreeModel->clear();

}

void ItemWindow::on_button_add_clicked(/* Glib::ustring getValue */)
{
	//std::cout << "Get Value [" << getValue << "]\n" << std::endl; 
	std::cout << "Get Value [ " << m_Entry.get_text() << " ]\n" << std::endl; 

	/*
	ModelColumns m_Columns;
	m_Columns.m_col_id = count_item;
	m_Columns.m_col_name = "Random";
	m_Columns.m_col_number = m_Entry.get_text();
	m_Columns.m_col_percentage = 0;
	m_vecItems.push_back(m_Columns);
	
	liststore_add_item(m_Columns);
	*/

	CellItem_Product foo;
	foo.m_col_id = index_count;
	foo.m_col_name = m_Entry.get_text();
	m_vecItems.push_back(foo);
	
	liststore_add_item(foo);
	file_add_item(foo);
  
	index_count++;

	// Init Entry to NULL
	m_Entry.set_text("");

}

void ItemWindow::on_button_remove_clicked()
{
	Glib::RefPtr<Gtk::TreeSelection> refSelection = m_TreeView.get_selection();

	if(const Gtk::TreeModel::iterator iter = refSelection->get_selected())
	{
	std::cout << "iter        [ " << iter << " ]\n" << std::endl; //
		const Gtk::TreeModel::Path path(iter);
		const unsigned int index = path.front();
	std::cout << "index       [ " << index << " ]\n" << std::endl; //

		// Remove item from ListStore:
		m_refTreeModel->erase(iter);

		// Remove item from vecItems.
		if(index < m_vecItems.size())
			m_vecItems.erase(m_vecItems.begin() + index);

 		file_remove_item(index);
		index_count--;
	}
	std::cout << "Remove Item [ " << m_Entry.get_text() << " ]\n" << std::endl; 

}


void ItemWindow::file_add_item(const CellItem_Product& foo)
{
	char str_log[60]="";
	FILE *pfile_write;

	pfile_write = fopen(LIST_LOG_LOCATE, "a+t");
	if(pfile_write == NULL)
		printf("Fail to open %s file!\n", LIST_LOG_LOCATE);


	/* Conversion Glib::ustring to std::string using data() method. */
	sprintf(str_log, "%4d\t%s", foo.m_col_id, foo.m_col_name.data());
	fprintf(pfile_write, "%s\n", str_log);

	fclose(pfile_write);
}

void ItemWindow::file_remove_item(unsigned int index)
{
	int i=0, j=1;
	char str_log[60]="";

	FILE *pfile_read;
	FILE *pfile_write;

	pfile_read = fopen(LIST_LOG_LOCATE, "rt");
	if(pfile_read == NULL)
	{
		printf("log file not found......\n");
		pfile_read = fopen(LIST_LOG_LOCATE, "a+t");
	}

	pfile_write = fopen(LIST_TMP_LOCATE, "w+t");
	if(pfile_write == NULL)
		printf("log file not found......\n");


	for(i=0;;i++)
	{
		if(feof(pfile_read))
			break;

		fgets(str_log, sizeof(str_log), pfile_read);

		if(i == index )
			continue;

		fputs(str_log, pfile_write);

		sprintf(str_log, "");
	}
	fclose(pfile_read);
	fclose(pfile_write);
	unlink(LIST_LOG_LOCATE);


	pfile_read = fopen(LIST_TMP_LOCATE, "rt");
	if(pfile_read == NULL)
		printf("log file not found......\n");

	pfile_write = fopen(LIST_LOG_LOCATE, "w+t");
	if(pfile_write == NULL)
		printf("log file not found......\n");

	for(i=0;;i++)
	{
		if(feof(pfile_read))
			break;

		fgets(str_log, sizeof(str_log), pfile_read);
		fputs(str_log, pfile_write);

		sprintf(str_log, "");
	}
	fclose(pfile_read);
	fclose(pfile_write);

	return;
}
